package com.hualan.bean;


import com.hualan.enums.ResponseEnum;

/**
 * Created with IntelliJ IDEA.
 * Stay hungry, Stay foolish
 * User: elliott
 * Date: 2018-03-24
 * Time: 16:08
 * 通用处理返回结果
 */
public class JsonResult<T> {

    // 请求返回码
    private Integer status;

    // 提示信息
    private String message;

    // 错误信息
    private String errorMessage;

    // 返回的数据
    private T data;

    public JsonResult() {
    }

    /**
     * 操作失败 返回错误信息
     * @param status
     * @param message
     * @param errorMessage
     */
    public JsonResult(Integer status, String message, String errorMessage) {
        this.status = status;
        this.message = message;
        this.errorMessage = errorMessage;
    }


    /**
     * 操作成功 无数据信息
     * @param status
     * @param message
     */
    public JsonResult(Integer status, String message) {
        this.status = status;
        this.message = message;
    }

    /**
     * 操作成功 返回数据信息
     * @param status
     * @param message
     * @param data
     */
    public JsonResult(Integer status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }


    public JsonResult(ResponseEnum responseEnum) {
        this.status = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

    public JsonResult(ResponseEnum responseEnum, T data) {
        this.status = responseEnum.getCode();
        this.message = responseEnum.getMessage();
        this.data = data;
    }

    public static JsonResult success(){
        return new JsonResult(ResponseEnum.SUCCESS);
    }

    public static <T> JsonResult<T> success(T data){
        return new JsonResult<T>(ResponseEnum.SUCCESS, data);
    }

    public static <T> JsonResult<T> success(int code, String msg){
        return new JsonResult<T>(code, msg);
    }

    public static JsonResult error(int code, String msg){
        return new JsonResult(code,msg);
    }

    public static JsonResult error(ResponseEnum responseEnum){
        return new JsonResult(responseEnum);
    }

    public static JsonResult error(ResponseEnum responseEnum, Object data){
        return new JsonResult<Object>(responseEnum, data);
    }

    public static JsonResult errorParams(String msg){
        return new JsonResult(ResponseEnum.INCORRECT_PARAMS.getCode(), msg);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
